package hsmoto.UIContainer
{
	import flash.display.DisplayObject;
	import flash.events.Event;
	
	import hsmoto.core.UIComponent;
	import hsmoto.supportClass.BasicLayout;
	import hsmoto.supportClass.LayoutBase;
	import hsmoto.supportClass.LayoutManager;

	/**
	 * 自动布局的容器的基类 ，会定时检查子对象的大小和位置变化，并在合适的时候更新子对象位置
	 * @author Zhang
	 * 
	 */	
	public class Group extends UIComponent
	{
		private var _autoLayout:Boolean;
		/**
		 *用于布局 
		 */		
		private var _layout:LayoutBase;
		
		public function Group()
		{
			_layout = createLayout();
			_layout.targetGroup = this;
			_autoLayout = true;
		}
		
		protected function createLayout():LayoutBase
		{
			return new BasicLayout();
		}
		/**
		 * 是否启用自己布局，如果不启用需要手动刷新 
		 */		
		public function set autoLayout(value:Boolean):void
		{
			_autoLayout = value;
			if(value)
			{
				if(this.stage)
					addListener();
			}
			else
			{
				removeListener();
			}
		}
		private var _listenerAdded:Boolean;
		private function addListener():void
		{
			if(_listenerAdded)
				return;
			this.addEventListener(Event.EXIT_FRAME, checkSize, false, 2);
			_listenerAdded = true;
		}
		private function removeListener():void
		{
			this.removeEventListener(Event.EXIT_FRAME, checkSize);
			_listenerAdded = false;
		}
		/**
		 * 通过EXIT_FRAME定时检查自己的大小变化
		 */		
		protected function checkSize(event:Event):void
		{
			if(_alreadyInList)
				return;
			if(_layout.needUpdate())
				addToUpdateDisplayList();
		}
		
		override protected function added(event:Event):void
		{
			super.added(event);
			if(_autoLayout)
				addListener();
		}
		
		override protected function removed(event:Event):void
		{
			super.removed(event);
			removeListener();
		}
		
		private var _uiChildren:Array = new Array();
		
		override protected function childAdd(child:DisplayObject):void
		{
			if(child is UIComponent)_uiChildren.push(child as UIComponent);
			addToUpdateDisplayList();
		}
		
		override protected function childRemove(child:DisplayObject):void
		{
			if(child is UIComponent)_uiChildren.splice(_uiChildren.indexOf(child), 1);
			addToUpdateDisplayList();
		}
		/**
		 * 是否已经在待更新列表 
		 */		
		private var _alreadyInList:Boolean;
		override public function addToUpdateDisplayList():void
		{
			if(_alreadyInList)
				return;
			if(LayoutManager.addToUpdateList(this))
				super.addToUpdateDisplayList();
			_alreadyInList = true;
		}
		/**
		 * 更新子对象的位置 
		 */		
		public function updateDisplayList():void
		{
			_layout.updateDisplayList();
			_alreadyInList = false;
		}
		/**
		 *  布局方式
		 */		
		public function get layout():LayoutBase
		{
			return _layout;
		}
		
		public function set layout(value:LayoutBase):void
		{
			_layout = value;
			_layout.targetGroup = this;
			addToUpdateDisplayList();
		}
		/**
		 *类型为UIComponent的子元件 
		 */		
		public function get uiChildren():Array
		{
			return _uiChildren;
		}

		public function get autoLayout():Boolean
		{
			return _autoLayout;
		}
	}
}